package ch.jamme.conf.def;

import java.lang.reflect.*;
import java.util.*;

import org.jdom.*;

import ch.jamme.conf.*;
import ch.jamme.util.*;

/**
 * Create an empty Object that fits the specified Field starting from its description expressed as a JDOM Element
 * @author Mario Fusco
 */
public class DefaultObjectCreator implements ObjectCreator {

	/**
	 * Creates an Object represented as a JDOM Element and compatible with the given Field
	 * @param element The JDOM Element representing the Object
	 * @param objectClass The Class of the object to be created
	 * @param field The Field where the object will need to be set
	 * @return An Object with the given representation as JDOM Element and compatible with the given Field
	 */
	public Object createObject(Element element, Class<?> objectClass, Field field) {
		if (!objectClass.isInterface()) return IntrospectionUtil.getObjectByEmptyConstructor(objectClass);
		if (SortedMap.class.isAssignableFrom(objectClass)) return new TreeMap<Object, Object>();
		if (Map.class.isAssignableFrom(objectClass)) return new HashMap<Object, Object>();
		if (SortedSet.class.isAssignableFrom(objectClass)) return new TreeSet<Object>();
		if (Set.class.isAssignableFrom(objectClass)) return new HashSet<Object>();
		if (Collection.class.isAssignableFrom(objectClass)) return new ArrayList<Object>();
		return null;
	}
}
